from django.db import models

# Create your models here.
class CatesModel(models.Model):
    name = models.CharField(max_length=20)
    addtime = models.DateTimeField(auto_now_add=True)
    class Meta:
        db_table = 'cates'

class QuestionsModel(models.Model):
    title = models.CharField(max_length=100)
    answer = models.TextField()
    addtime = models.DateTimeField(auto_now_add=True)
    cate = models.ForeignKey(CatesModel, on_delete=models.CASCADE)
    class Meta:
        db_table = 'questions'

# class UploadedFile(models.Model):
#     file = models.FileField(upload_to='uploads/')
#     uploaded_at = models.DateTimeField(auto_now_add=True)
#     class Meta:
#         db_table = 'uploaded_files'


# 商品表
# id  name  catesid
class Goods(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    content = models.TextField()
    sales = models.IntegerField(default=0)
    features = models.TextField()  # 存储商品特征，可以用JSON格式
    image = models.ImageField(upload_to='goods_images/', blank=True, null=True)

    class Meta:
        db_table = 'goods'

class BrowseHistory(models.Model):
    user_id = models.CharField(max_length=36)
    product = models.ForeignKey(Goods, on_delete=models.CASCADE)
    add_time = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.user_id} viewed {self.product.name}"
    class Meta:
        db_table = 'browse_history'